#include "./Protocol/Protocol.hpp"
#include "./Protocol/ProtocolLogin.hpp"
#include "./Protocol/ProtocolRegister.hpp"
#include "./Protocol/ProtocolCommonResp.hpp"

class ProtocolFactory
{
public:
    static ProtocolPtr createProtocol(Protocol::Type type)
    {
        switch (type)
        {
        case Protocol::LoginReq:
            return std::make_shared<ProtocolLoginReq>();
        case Protocol::RegisterReq:
            return std::make_shared<ProtocolRegisterReq>();
        case Protocol::CommonResp:
            return std::make_shared<ProtocolCommonResp>();
        default:
            return nullptr;
        }
    }

    static ProtocolPtr createProtocolFromJson(const std::string &json)
    {
        Json::Reader reader;
        Json::Value root;
        if(!reader.parse(json, root))
        {
            DLOG("解析错误");
            return nullptr;
        }

        Protocol::Type type = (Protocol::Type)root["type"].asInt();
        ProtocolPtr ret = createProtocol(type);
        ret->deserialize(root);


        return ret;
    }
};